/*-----------------------------------------------
 * BookEZ - Booking System Project
 *-----------------------------------------------*/
package bookez.model.dao.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import bookez.model.businessobject.User;
import bookez.model.dao.UserDao;
import bookez.model.dao.generic.GenericDaoHibernateImpl;
import bookez.model.exception.DataAccessLayerException;

public class UserDaoHibernateImpl extends GenericDaoHibernateImpl<User, Long> implements UserDao {
	
	public UserDaoHibernateImpl( Class<User> type ) { 
		super( type ); 
	}//end contructor
	
	/**
	 *  Retreive User by User.username
	 */
	@Override
	public User findByUsername( String username ) throws DataAccessLayerException {
		// TODO Auto-generated method stub'
		
		User user = null;
		
		try {	
			Query query = getSession().getNamedQuery( User.NamedQueries.FIND_BY_USERNAME );
			query.setParameter( "username", username );
			
			user = (User) query.uniqueResult();
			
			return user;
		}//end try
		
		catch( HibernateException hex ) {
			throw new DataAccessLayerException( "Hibernate cannot retreive user.", hex );
		}//end catch HibernateException
		
		catch ( Exception ex ) { 
			throw new DataAccessLayerException( ex.getMessage(), ex );
		}//end catch
	}//end method findByUsername
	
}//end class UserDaoHibernateImpl
